package com.test.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.request.AlipayUserInfoShareRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayUserInfoShareResponse;
import com.test.config.AliLoginConfig;
import com.test.entity.AliLogin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("alilogin")
public class AliLoginController {

    @Resource
    AliLoginConfig aliPayConfig;

    private static final String GATEWAY_URL = "https://openapi.alipay.com/gateway.do";
    private static final String FORMAT = "JSON";
    private static final String CHARSET = "utf-8";
    private static final String SIGN_TYPE = "RSA2";

    //登录、注册接口（根据用户在支付宝在的数据来创建、查询用户在自己应用的消息）
    @GetMapping("/login")
    public String login(AliLogin aliLogin) throws AlipayApiException {
        System.out.println(JSON.toJSONString(aliLogin));
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, aliPayConfig.getAppId(),
                aliPayConfig.getAppPrivateKey(), FORMAT, CHARSET, aliPayConfig.getAlipayPublicKey(), SIGN_TYPE);

        // 构造请求参数以调用接口
        AlipaySystemOauthTokenRequest alipaySystemOauthTokenRequest = new AlipaySystemOauthTokenRequest();
        // 设置授权码
        alipaySystemOauthTokenRequest.setCode(aliLogin.getAuth_code());
        // 设置授权方式
        alipaySystemOauthTokenRequest.setGrantType("authorization_code");

        AlipaySystemOauthTokenResponse systemOauthTokenResponse = alipayClient.execute(alipaySystemOauthTokenRequest);
        System.out.println(systemOauthTokenResponse.getBody());
        AlipayUserInfoShareRequest alipayUserInfoShareRequest = new AlipayUserInfoShareRequest();
        JSONObject jsonObject = JSON.parseObject(systemOauthTokenResponse.getBody());
        // 获取 alipay_system_oauth_token_response 对象
        JSONObject responseObject = jsonObject.getJSONObject("alipay_system_oauth_token_response");
        // 获取 access_token 值
        String access_token = responseObject.getString("access_token");
        AlipayUserInfoShareResponse userInfoShareResponse = alipayClient.execute(alipayUserInfoShareRequest, access_token);

        jsonObject= JSON.parseObject(userInfoShareResponse.getBody());
        responseObject = jsonObject.getJSONObject("alipay_user_info_share_response");
        String open_id= responseObject.getString("open_id");//用户在支付宝的唯一标识符
        String nick_name = responseObject.getString("nick_name");//用户在支付宝昵称
        String avatar = responseObject.getString("avatar");//用户在支付宝头像地址
        String msg = responseObject.getString("msg");//接口状态信息，为‘Success’时是正常的
        System.out.println(JSON.toJSONString(jsonObject));
        return "success";
    }
}
